 {include file="public/header" type='pear-container'/}
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
				        <div class="layui-form-item layui-inline">
                           <label class="layui-form-label">会员ID</label>
                           <div class="layui-input-inline">
                               <input type="text" name="user_id" placeholder="" class="layui-input">
                           </div>
                        </div>   
                        <div class="layui-form-item layui-inline">
                            <label class="layui-form-label">通道类型</label>
                            <div class="layui-input-inline">
                               <select name="type" id="type" lay-filter="type">
                                    <option value="">请选择一个类型</option>
                                    <option value="alipay">支付宝</option>
                                    <option value="wxpay">微信</option>
                                    <option value="qqpay">QQ</option>
                                </select>  
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline">
                            <label class="layui-form-label">通道名称</label>
                            <div class="layui-input-inline">
                               <select name="code" id="code">
                                   <option value="">请选择一个通道</option>
                                   <?php foreach ($channel as $key => $value): ?>
                                       <option value="{$value['code']}">{$value['name']}</option>
                                   <?php endforeach; ?>
                                </select>  
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline">
                       <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="query">
                           <i class="layui-icon layui-icon-search"></i>
                           查询
                       </button>
                       <button type="reset" class="pear-btn pear-btn-md">
                           <i class="layui-icon layui-icon-refresh"></i>
                           重置
                       </button>
                       </div>
                    </div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="account" lay-filter="account"></table>
			</div>
		</div>

		<script type="text/html" id="toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-delete"></i>
		        批量删除
		    </button>
		    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="allRemove">
		        <i class="layui-icon layui-icon-delete"></i>
		        一键删除离线账号
		    </button>
		</script>

        
               <script type="text/html" id="status">
                   <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="在线|离线" lay-filter="status" {{# if(d.status==1){ }} checked {{# } }}>
               </script>
               <script type="text/html" id="is_status">
                   <input type="checkbox" name="is_status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="is_status" {{# if(d.is_status==1){ }} checked {{# } }}>
               </script>

		<script type="text/html" id="options">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>
		{include file="public/footer"/}
        <script>
        layui.use(['table', 'form', 'jquery'], function() {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let MODULE_PATH = "{$Request.root}/ypay.account/";
            
            form.on("select(type)", function (data) {
                var postdata ={
                    type: $('#type').val(),
                }
                
                $.getJSON(MODULE_PATH + "/getChannel",postdata,function(data){
                    if (data.code) {
                            var list= data.channel;
                            var nr= '<option value="">请选择一个通道</option>';
                            if(list){
                                    
                                for(var i = 0; i < list.length; i++) {
                                    nr += "<option value='" + list[i].code + "'>" + list[i].name + "</option>"
                                };
                            }
                            $("#code").html(nr);
                            form.render('select');
                    }
                },true);
                
            });
            
            let cols = [
                [{
                        type: 'checkbox'
                    }, {
                       field: "code_name",
                       title: "通道名称",
                       unresize: "true",
                       minWidth: 105,
                       align: "center"
                   }, {
                       field: "type",
                       title: "通道类型",
                       unresize: "true",
                       align: "center",
                       templet:function(d){
                           if(d.type == 'wxpay'){
                               return '<span style="color:#16b777;">微信</span>';
                           }else if(d.type == 'qqpay'){
                               return '<span style="color:#ff5722;">QQ</span>';
                           }else{
                               return '<span style="color:#1e9fff;">支付宝</span>';
                           }
                       }
                   }, {
                       field: "user_id",
                       title: "会员ID",
                       unresize: "true",
                       align: "center"
                   },{
                        field: 'biaoshi', title: '账户标识', width: 200, align: 'center', templet: function (d) {
                            if (d.code == 'alipay_grmg' || d.code == 'alipay_pc' || d.code == 'alipay_app') {
                                return d.zfb_pid;
                            }else if(d.code == 'alipay_dmf'){
                                return d.wxname;
                            }
                            else if (d.code == 'wxpay_cloud' || d.code == 'wxpay_ipad' || d.code == 'wxpay_cloudzs' || d.code == 'wxpay_skd') {
                                return d.wx_guid;
                            }
                            else if (d.code == 'qqpay_mg' || d.code == 'qqpay_cloud' || d.code == 'qqpay_wzq') {
                                return d.qq;
                            }
                            else {
                                return d.wxname;
                            }
                        }
                    }, {
                       field: "status",
                       title: "状态",
                       unresize: "true",
                       align: "center",
                       templet:"#status"
                   }, {
                       field: "is_status",
                       title: "是否启用",
                       unresize: "true",
                       align: "center",
                       templet:"#is_status"
                   }, {
                       field: "succcount",
                       title: "收款笔数",
                       unresize: "true",
                       align: "center"
                   }, {
                       field: "succprice",
                       title: "收款金额",
                       unresize: "true",
                       align: "center"
                   }, {
                       field: "memo",
                       title: "备注",
                       unresize: "true",
                       align: "center"
                   }, {
                        title: '操作',
                        toolbar: '#options',
                        unresize: true,
                        align: 'center',
                        width:180,
                    }
                ]
            ]

            table.render({
                elem: '#account',
                id : 'account',
                url: MODULE_PATH + 'index',
                page: true,
                cols: cols,
                cellMinWidth: 100,
                skin: 'line',
                toolbar: '#toolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });

            table.on('tool(account)', function(obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                }
            });

            table.on('toolbar(account)', function(obj) {
                if (obj.event === 'add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                } else if (obj.event === 'batchRemove') {
                    window.batchRemove(obj);
                } else if (obj.event === 'allRemove') {
                    window.allRemove(obj);
                }
            });

            form.on('submit(query)', function(data) {
                table.reload('account', {
                    where: data.field,
                    page:{curr: 1}
                })
                
                return false;
            });
            
               form.on("switch(status)", function(data) {
                   var status = data.elem.checked?1:0;
                   var id = this.value;
                   var load = layer.load();
                   $.post(MODULE_PATH + "status",{status:status,id:id},function (res) {
                       layer.close(load);
                       //判断有没有权限
                       if(res && res.code==999){
                           layer.msg(res.msg, {
                               icon: 5,
                               time: 2000, 
                           })
                           return false;
                       }else if (res.code==200){
                           layer.msg(res.msg,{icon:1,time:1500})
                       } else {
                           layer.msg(res.msg,{icon:2,time:1500},function () {
                               $(data.elem).prop("checked",!$(data.elem).prop("checked"));
                               form.render()
                           })
                       }
                   })
               });
               form.on("switch(is_status)", function(data) {
                   var status = data.elem.checked?1:2;
                   var id = this.value;
                   var load = layer.load();
                   $.post(MODULE_PATH + "is_status",{is_status:status,id:id},function (res) {
                       layer.close(load);
                       //判断有没有权限
                       if(res && res.code==999){
                           layer.msg(res.msg, {
                               icon: 5,
                               time: 2000, 
                           })
                           return false;
                       }else if (res.code==200){
                           layer.msg(res.msg,{icon:1,time:1500})
                       } else {
                           layer.msg(res.msg,{icon:2,time:1500},function () {
                               $(data.elem).prop("checked",!$(data.elem).prop("checked"));
                               form.render()
                           })
                       }
                   })
               });
            //弹出窗设置 自己设置弹出百分比
            function screen() {
                if (typeof width !== 'number' || width === 0) {
                width = $(window).width() * 0.8;
                }
                if (typeof height !== 'number' || height === 0) {
                height = $(window).height() * 0.5;
                }
                return [width + 'px', height + 'px'];
            }

            window.add = function() {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '新增账号管理',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'add'
                });
            }

            window.edit = function(obj) {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '查看账号信息',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'edit/id/'+obj.data['id']
                });
            }


            window.remove = function(obj) {
                layer.confirm('确定要删除该通道账号？', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url:MODULE_PATH + 'remove',
                        data:{id:obj.data['id']},
                        dataType: 'json',
                        type: 'POST',
                        success: function(res) {
                            layer.close(loading);
                            //判断有没有权限
                            if(res && res.code==999){
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000, 
                                })
                                return false;
                            }else if (res.code==200) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    obj.del();
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.batchRemove = function(obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                var ids = []
                var hasCheck = table.checkStatus('account')
                var hasCheckData = hasCheck.data
                if (hasCheckData.length > 0) {
                    $.each(hasCheckData, function (index, element) {
                        ids.push(element.id)
                    })
                }
                layer.confirm('确定要删除这些账号管理', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url:MODULE_PATH + 'batchRemove',
                        data:{ids:ids},
                        dataType: 'json',
                        type: 'POST',
                        success: function(res) {
                            layer.close(loading);
                            //判断有没有权限
                            if(res && res.code==999){
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000, 
                                })
                                return false;
                            }else if (res.code==200) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    table.reload('account');
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }
            
            window.allRemove = function(obj) {
                layer.open({
        type: 1,
        area: ['50%', '30%'],
        resize: false,
        shadeClose: true,
        title: '清除离线账户',
        content: `
          <div class="layui-form" style="margin: 16px;">
            <div class="demo-login-container">
              <div class="layui-form-item">
                <div class="layui-row">
                  <div class="layui-col-xs12">
                    <div class="layui-input-wrap">
                               <select name="type">
                                    <option value="">请选择一个类型</option>
                                    <option value="alipay">支付宝</option>
                                    <option value="wxpay">微信</option>
                                    <option value="qqpay">QQ</option>
                                </select>  
                    </div>
                  </div>
                </div>
              </div>
              <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="clear">清理</button>
              </div>
            </div>
          </div>
        `,
        success: function(){
          // 对弹层中的表单进行初始化渲染
          form.render();
          // 表单提交事件
          form.on('submit(clear)', function(data){
            var field = data.field; // 获取表单字段值
            // 此处可执行 Ajax 等操作
            $.ajax({
 					data: JSON.stringify(data.field),
 					url: MODULE_PATH + 'remove_line',
 					dataType: 'json',
 					contentType: 'application/json',
 					type: 'post',
 					success: function(res) {
 						if (res.code == 200) {
 							layer.msg(res.msg, {
 								icon: 1,
 								time: 1000,
 							}, function() {
 								$(window.parent.document).find("iframe")[0].contentWindow
 									.location.reload(true);

 							})

 							return false;
 						} else {
 							layer.msg(res.msg, {
 								icon: 5,
 								time: 1000
 							});
 						}
 					}
 				})
            return false; // 阻止默认 form 跳转
          });
        }
      });
                // layer.confirm('确定要清除所有离线账号嘛', {
                //     icon: 3,
                //     title: '提示'
                // }, function(index) {
                //     layer.close(index);
                //     let loading = layer.load();
                //     $.ajax({
                //         url:MODULE_PATH + 'allRemove',
                //         type: 'POST',
                //         success: function(res) {
                //             layer.close(loading);
                //             //判断有没有权限
                //             if(res && res.code==999){
                //                 layer.msg(res.msg, {
                //                     icon: 5,
                //                     time: 2000, 
                //                 })
                //                 return false;
                //             }else if (res.code==200) {
                //                 layer.msg(res.msg, {
                //                     icon: 1,
                //                     time: 1000
                //                 }, function() {
                //                     table.reload('account');
                //                 });
                //             } else {
                //                 layer.msg(res.msg, {
                //                     icon: 2,
                //                     time: 1000
                //                 });
                //             }
                //         }
                //     })
                // });
            }
            
            window.refresh = function(param) {
                table.reload('account');
            }
        })
    </script>
    
	</body>
</html>
